//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackDeltaCalculator.h"
using namespace Cephei::QL::Experimental::Fx;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (QL::Option::TypeEnum ot, QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum dt, Double spot, Double dDiscount, Double fDiscount, Double stdDev) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phBlackDeltaCalculator = NULL;
#endif
        QuantLib::Option::Type _ot = (QuantLib::Option::Type)ot ;
        QuantLib::DeltaVolQuote::DeltaType _dt = (QuantLib::DeltaVolQuote::DeltaType)dt ;
        QuantLib::Real _spot = (QuantLib::Real)ValueHelper::Convert (spot); //d
        QuantLib::DiscountFactor _dDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (dDiscount); //d
        QuantLib::DiscountFactor _fDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (fDiscount); //d
        QuantLib::Real _stdDev = (QuantLib::Real)ValueHelper::Convert (stdDev); //d
        _ppBlackDeltaCalculator = new boost::shared_ptr<QuantLib::BlackDeltaCalculator> (new QuantLib::BlackDeltaCalculator ( _ot,  _dt,  _spot,  _dDiscount,  _fDiscount,  _stdDev ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (boost::shared_ptr<QuantLib::BlackDeltaCalculator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackDeltaCalculator = NULL;
#endif
	_ppBlackDeltaCalculator = &childNative;
    
}
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (QuantLib::BlackDeltaCalculator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBlackDeltaCalculator = NULL;
#endif
	_ppBlackDeltaCalculator = new boost::shared_ptr<QuantLib::BlackDeltaCalculator> (&childNative);
    
    _BlackDeltaCalculatorOwner = owner;
    
}

Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (CBlackDeltaCalculator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackDeltaCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackDeltaCalculator = new boost::shared_ptr<QuantLib::BlackDeltaCalculator> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackDeltaCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CBlackDeltaCalculator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (QuantLib::Handle<QuantLib::BlackDeltaCalculator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBlackDeltaCalculator = &childNative;
	_ppBlackDeltaCalculator = &static_cast<boost::shared_ptr<QuantLib::BlackDeltaCalculator>>(childNative.currentLink());
    
    _BlackDeltaCalculatorOwner = owner;
}
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (QuantLib::Handle<QuantLib::BlackDeltaCalculator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBlackDeltaCalculator = &childNative;
	_ppBlackDeltaCalculator = &static_cast<boost::shared_ptr<QuantLib::BlackDeltaCalculator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CBlackDeltaCalculator (QuantLib::BlackDeltaCalculator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackDeltaCalculator = NULL;
#endif
	_ppBlackDeltaCalculator = new boost::shared_ptr<QuantLib::BlackDeltaCalculator> (new QuantLib::BlackDeltaCalculator (childNative));
    
}
#endif

Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::~CBlackDeltaCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBlackDeltaCalculator != NULL)
    {
	    delete _ppBlackDeltaCalculator;
        _ppBlackDeltaCalculator = NULL;
    }
}
Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::!CBlackDeltaCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBlackDeltaCalculator != NULL)
    {
	    delete _ppBlackDeltaCalculator;
    }
}
QuantLib::BlackDeltaCalculator& Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::GetReference ()
{
    if (_ppBlackDeltaCalculator == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackDeltaCalculator;
}
boost::shared_ptr<QuantLib::BlackDeltaCalculator>& Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::GetShared ()
{
    if (_ppBlackDeltaCalculator == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackDeltaCalculator;
}
QuantLib::BlackDeltaCalculator* Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::GetPointer ()
{
    if (_ppBlackDeltaCalculator == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackDeltaCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackDeltaCalculator>& Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::GetHandle ()
{
	if (_phBlackDeltaCalculator == NULL)
	{
		_phBlackDeltaCalculator = new Handle<QuantLib::BlackDeltaCalculator> (*_ppBlackDeltaCalculator);
	}
	return *_phBlackDeltaCalculator;
}
#endif
bool Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::HasNative () 
{
	return (_ppBlackDeltaCalculator != NULL);
}

Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::AtmStrike (QL::Experimental::Fx::DeltaVolQuote::AtmTypeEnum atmT)
{
    try
    {
        QuantLib::DeltaVolQuote::AtmType _atmT = (QuantLib::DeltaVolQuote::AtmType)atmT ;
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->atmStrike ( _atmT );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CumD1 (Double strike)
{
    try
    {
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->cumD1 ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::CumD2 (Double strike)
{
    try
    {
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->cumD2 ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::DeltaFromStrike (Double strike)
{
    try
    {
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->deltaFromStrike ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::ND1 (Double strike)
{
    try
    {
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->nD1 ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::ND2 (Double strike)
{
    try
    {
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->nD2 ( _strike );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Fx::IBlackDeltaCalculator^ Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::SetDeltaType (QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum dt)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::DeltaVolQuote::DeltaType _dt = (QuantLib::DeltaVolQuote::DeltaType)dt ;
    	(*_ppBlackDeltaCalculator)->setDeltaType ( _dt );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Fx::IBlackDeltaCalculator^ Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::SetOptionType (QL::Option::TypeEnum ot)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Option::Type _ot = (QuantLib::Option::Type)ot ;
    	(*_ppBlackDeltaCalculator)->setOptionType ( _ot );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CBlackDeltaCalculator::StrikeFromDelta (Double delta)
{
    try
    {
        QuantLib::Real _delta = (QuantLib::Real)ValueHelper::Convert (delta); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackDeltaCalculator)->strikeFromDelta ( _delta );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Fx::IBlackDeltaCalculator^ Cephei::QL::Experimental::Fx::CBlackDeltaCalculator_Factory::Create (QL::Option::TypeEnum ot, QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum dt, Double spot, Double dDiscount, Double fDiscount, Double stdDev)
{
    return REFNEW CBlackDeltaCalculator ( ot,  dt,  spot,  dDiscount,  fDiscount,  stdDev);
}
